package com.zhuss.outer.processor;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.vo.ListTopicTreeVo;
import com.zhuss.bean.vo.outer.outer.DetailOuterTopicVo;
import com.zhuss.enums.Area;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;
import com.zhuss.outer.db.dao.ext.OuterTopicDao;
import com.zhuss.util.ListBean;


/**
 * 查询外包消息Topic processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("outerTopicRead")
public class OuterTopicRead {
	
	@Autowired
	private OuterTopicDao outerTopicDao;
	
	
	public List<DetailOuterTopicVo> listTopic(Long outerId, Long parent) {
		List<DetailOuterTopicVo> list = outerTopicDao.listTopic(outerId, parent);
		return list;
	}
	
	
	/**
	 * 外包消息Topic子集列表
	 * @param outerTopicId
	 * @return
	 * @throws WpsException
	 */
	public List<Long> listSon(List<Long> outerTopicIdList) throws WpsException {
		List<Long> result = outerTopicDao.listSon(outerTopicIdList);
		return result;
	}
	
	/**
	 * 外包消息Topic详情
	 * @param outerTopicId
	 * @return
	 * @throws WpsException
	 */
	public Object detail(Long outerTopicId) throws WpsException {
		Object result = outerTopicDao.detail(outerTopicId);
		if (result == null) {
			throw new WpsException(ExceptionCode.数据不存在);
		}
		return result;
	}
	
	/**
	 * 评论树
	 * @param outerId
	 * @return
	 */
	public Object listTopicTree(Long outerId) {
		List<com.zhuss.outer.db.entity.OuterTopic> topicList = outerTopicDao.listAll(Area.东区.key(), outerId);
		List<ListTopicTreeVo> result = new ArrayList<>();
		transferTree(topicList, result);
		return result;
	}
	
	/**
	 * 转换树
	 * @param topicList
	 * @param result
	 */
	private void transferTree(List<com.zhuss.outer.db.entity.OuterTopic> topicList, List<ListTopicTreeVo> result) {
		for (com.zhuss.outer.db.entity.OuterTopic topic : topicList) {
			if (topic.getParent() == null || topic.getParent().equals(0l)) {
				ListTopicTreeVo vo = new ListTopicTreeVo();
				vo.setOuterTopicId(topic.getOuterTopicId());
				vo.setTopicContent(topic.getTopicContent());
				vo.setTargetUserId(topic.getTargetUserId());
				vo.setParent(topic.getParent());
				vo.setCreateTime(topic.getCreateTime());
				vo.setCreator(topic.getCreator());
				vo.setCreatorId(topic.getCreatorId());
				vo.setCreatorUrl(topic.getCreatorUrl());
				result.add(vo);
			}
		}
		
		for (ListTopicTreeVo vo : result) {
			vo.setChildren(init(topicList, vo));
		}
	}
	
	/**
	 * 递归初始化树
	 * @param topicList
	 * @param vo
	 * @return
	 */
	private List<ListTopicTreeVo> init(List<com.zhuss.outer.db.entity.OuterTopic> topicList, ListTopicTreeVo vo) {
		ListBean<ListTopicTreeVo> listBean = new ListBean<>();
		for (com.zhuss.outer.db.entity.OuterTopic topic : topicList) {
			if (vo.getOuterTopicId().equals(topic.getParent())) {
				ListTopicTreeVo item = new ListTopicTreeVo();
				item.setOuterTopicId(topic.getOuterTopicId());
				item.setTopicContent(topic.getTopicContent());
				item.setTargetUserId(topic.getTargetUserId());
				item.setParent(topic.getParent());
				item.setCreateTime(topic.getCreateTime());
				item.setCreator(topic.getCreator());
				item.setCreatorId(topic.getCreatorId());
				item.setCreatorUrl(topic.getCreatorUrl());
				item.setChildren(init(topicList, item));
				listBean.build(item);
			}
		}
		return listBean.getList();
	}
	
	
	

}
